//
//  TZFXViewController.m
//  NewPro
//
//  Created by RYX on 2019/6/12.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "TZFXViewController.h"
#import "YHBToastView.h"
#import "YHBZDStore.h"
#import "TZJEDataModel.h"

@interface TZFXViewController ()
{
    __weak IBOutlet UILabel *la1;
    __weak IBOutlet UITextField *tf;
    __weak IBOutlet UILabel *la2;
    __weak IBOutlet UILabel *la3;
    __weak IBOutlet UILabel *la4;
    
    NSInteger min;
    NSInteger max;
}

@property (strong, nonatomic) YHBToastView *toastView;
@property (strong, nonatomic) UIView *bgView;

@end

@implementation TZFXViewController

- (YHBToastView *)toastView{
    
    if (!_toastView) {
        
        _toastView = [[YHBToastView alloc] initWithFrame:CGRectMake((ScreenWidth - 190)/2, -200, 190, 110)];
    }
    return _toastView;
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        [_bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didBgViewClick)]];
    }
    return _bgView;
}

- (void)didBgViewClick{
    
    MJWeakSelf
    weakSelf.toastView.y = - 200;
    [weakSelf.bgView removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    tf.keyboardType = UIKeyboardTypeNumberPad;
    [tf becomeFirstResponder];
    [self setNavBarWithTitle:@"调整返现"];
    
    [self.view insertSubview:self.toastView belowSubview:self.navigationController.navigationBar];

    [self loadData];
}

- (IBAction)onPostBtn:(UIButton *)sender {
    
    if (tf.text.length <= 0) {
        
        return;
    }
    [self.view endEditing:YES];
    
    if (![StrTool isPureInt:tf.text]) {
        
        return [self showMBPError:@"请输入整数"];
    }
    
    NSInteger a = [tf.text integerValue];
    
    if (a < min || a > max) {
        
        return [self showMBPError:@"输入金额超出可调整范围"];
    }
    
    MJWeakSelf
    YHBZDStore *store = [[YHBZDStore alloc] init];
    [store putfxjeWithSn:self.sn andAmount:tf.text Success:^{
        
        [weakSelf.view insertSubview:weakSelf.bgView belowSubview:weakSelf.toastView];
        
        [UIView animateWithDuration:0.25 animations:^{
            
            weakSelf.toastView.y = 150;
            weakSelf.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        }];
        
    } Failure:^(NSError * _Nonnull error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

- (void)loadData{
    
    MJWeakSelf
    YHBZDStore *store = [[YHBZDStore alloc] init];
    [store getTXDataWithSn:self.sn Success:^(TZJEDataModel * _Nonnull model) {
        
        self->la1.text = model.backAmount;
        self->la2.text = [NSString stringWithFormat:@"元（可调整范围%@-%@元）",model.min,model.max];
        self->la3.text = model.category;
        self->la4.text = model.sn;
        
        self->min = [model.min integerValue];
        self->max = [model.max integerValue];
    } Failure:^(NSError * _Nonnull error) {
       
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}
@end
